package org.jthompson.monsoon.factory;

import org.jthompson.monsoon.EventExecutor;
import org.jthompson.monsoon.EventMapping;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;

public class CGLibObjectFactory implements ObjectFactory
{

	private static CGLibObjectFactory instance = null;
	
	private CGLibObjectFactory() { /*NOOP */ }
	
	public static synchronized CGLibObjectFactory instance()
	{
		if( instance == null)
		{
			instance = new CGLibObjectFactory();
		}
		return instance;
	}
	
	private Callback callback = new EventExecutor();
	
	/**
	 * Create simple cglib proxy.
	 * 
	 * @param object
	 * @return
	 */
	public Object generateProxy(Object object)
	{
		Enhancer e = new Enhancer();
		e.setSuperclass(object.getClass());
		e.setCallback(callback);		
		return e.create();
	}
	
	/**
	 * Defaults to our eventExecutor.  Not sure why you'd ever 
	 * want to change this... but hey, I'm flexible.
	 * 
	 */
	public void setProxyCallback(Callback callback)
	{
		this.callback = callback;
	}
	
}
